import sbt._
import Keys._

object Build extends Build {


  /** main project containing main source code depending on slick and codegen project */
  lazy val mainProject = Project(
    id = "slick-codegen",
    base = file("."),

    settings = sharedSettings ++ Seq(
      unmanagedBase := baseDirectory.value / "libs",
      scalacOptions := Seq("-unchecked", "-Xexperimental", "-feature", "-deprecation", "-encoding", "utf8"),
      version := "1.0-M1",
      organization := "ws.corbin",
      name := "slick-codegen",
      slick <<= slickCodeGenTask, // register manual sbt command
      sourceGenerators in Compile <+= slickCodeGenTask, // register automatic code generation on every compile, remove for only manual use
      mainClass in run := Some("CodeGenRun")
    )
  ).dependsOn(codegenProject)


  /** codegen project containing the customized code generator */
  lazy val codegenProject = Project(
    id = "codegen",
    base = file("slick-code-generator"),
    settings = sharedSettings
  ).settings(resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/")


  // shared sbt config between main project and codegen project
  val sharedSettings = Seq(
    scalaVersion := "2.11.7",
    libraryDependencies ++= Seq(
      //  "com.typesafe.play" %% "play-slick" % "1.0.1",
      "com.typesafe.play" %% "play-json" % "2.4.2",
      "org.scala-lang" % "scala-library" % "2.11.7",
      "org.scala-lang" % "scala-compiler" % "2.11.7",
      "org.scala-lang" % "scala-reflect" % "2.11.7",
      "com.typesafe.slick" %% "slick" % "3.0.0",
      "com.typesafe.slick" %% "slick-codegen" % "3.0.0",
      "mysql" % "mysql-connector-java" % "5.1.35",
      "org.slf4j" % "slf4j-api" % "1.7.12",
      "joda-time" % "joda-time" % "2.7",
      "com.zaxxer" % "HikariCP" % "2.3.9"
    )
  )


  //sourceGenerators in Compile <+= slickCodeGenTask
  //
  lazy val slick = TaskKey[Seq[File]]("slick-codegen")
  lazy val slickCodeGenTask = ( /*scalaSource in Compile,*/ dependencyClasspath in Compile, runner in Compile, streams) map { (/*dir,*/ cp, r, s) =>
    toError(r.run("SlickCodeGenerator", cp.files, Array[String](), s.log))
    val f = new File("com.example.models.Models.scala")
    Seq(file(f.getAbsolutePath))
  }


}